package cn.itsource.hrm.util;

import cn.itsource.hrm.exception.GloabException;
import org.springframework.lang.Nullable;
import org.springframework.util.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class ValidityUtils {
   public static final String EMAIL_REGEX1 = "^([a-z0-9A-Z]+[-|\\.]?)+[a-z0-9A-Z]@([a-z0-9A-Z]+(-[a-z0-9A-Z]+)?\\.)+[a-zA-Z]{2,}$";
   
    public static boolean isEmail(String string) {
        if (string == null) {
            return false;
        }
        Pattern p;
        Matcher m;
        p = Pattern.compile(EMAIL_REGEX1);
        m = p.matcher(string);
        if (m.matches()) {
            return true;
        }else {
            return false;
        }
    }

    public static void isEmail(String email, String message) {
        if(!isEmail(email)){
            throw new GloabException(message);
        }
    }

    /**
     * 断言不为空，如果为空会报错
     * @param object
     * @param message
     */
    public static void isNotNull(@Nullable Object object, String message) {

        if (object == null) {
            throw new GloabException(message);
        }
        
        if(object instanceof String){
            if(StringUtils.isEmpty((String)object)) {
                throw  new GloabException(message);
            }
        }
        
        
    }

    /**
     * 不为空 则抛异常
     * @param object
     * @param mesg
     */
    public static void isNull(Object object, String mesg) {
        if (object != null) {
            throw new GloabException(mesg);
        }
    }

    /**
     * 断言不相同，相同则抛异常
     * @param int1
     * @param int2
     * @param msg
     */
    public static void isNotEquals(Integer int1, Integer int2, String msg) {
        if(int1.equals(int2)){
            throw new GloabException(msg);
        }
    }

    /**
     * 断言为真，假则抛异常
     * @param success
     * @param s
     */
    public static void isTrue(boolean success, String msg) {
        if(!success){
            throw new GloabException(msg);
        }
    }
}
